<?php
 /**
 * 将日期int，转成日期字符串
 * @input 20220118
 * @output 2022-01-18 
 * */
function dateIntToDateString($dateInt) {
    if (!is_int($dateInt)) {
        return '';
    }

    if ($dateInt < 10000000 || $dateInt > 99991231) {
        return '';
    }

    $dateString = (string) $dateInt;
    return substr($dateString, 0, 4) . '-' . substr($dateString, 4, 2) . '-' . substr($dateString, 6);
}

// Function to check the string is ends 
// with given substring or not
function endsWith($string, $endString)
{
    $len = strlen($endString);
    if ($len == 0) {
        return true;
    }
    return (substr($string, -$len) === $endString);
}

// Function to check string starting
// with given substring
function startsWith ($string, $startString)
{
    $len = strlen($startString);
    return (substr($string, 0, $len) === $startString);
}

/**
 * 用于生成相对的 URL 
 * */
function url($action, $controller = null) {
    $controller = is_null($controller) ? APP_CONTROLLER : $controller;
    
    // 根据用户是否使用了index.php进行访问来生成不同的 URL
    if (startsWith($_SERVER['REQUEST_URI'], $_SERVER['SCRIPT_NAME'])) {
        return $_SERVER['SCRIPT_NAME'] . "/$controller/$action";
    } else {
        return "/$controller/$action";
    }    
}
